package com.zjd.association.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjd.association.common.PageWrapper;
import com.zjd.association.entity.Members;
import com.zjd.association.entity.Users;
import com.zjd.association.mapper.MembersMapper;
import com.zjd.association.mapper.UsersMapper;
import com.zjd.association.service.UsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户 服务层实现类
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@Service
@RequiredArgsConstructor
public class UsersServiceImpl implements UsersService {

    private final MembersMapper membersMapper;

    private final UsersMapper usersMapper;

    @Override
    public void add(Users users) {
        usersMapper.insert(users);
    }

    @Override
    public void update(Users users) {
        usersMapper.updateById(users);
    }

    @Override
    public void delete(Users users) {
        usersMapper.deleteById(users);
    }

    /**
     * 检查指定的用户是否可以删除
     * 当用户不是活动成员可以进行删除
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public Boolean isRemove(String userId) {
        QueryWrapper<Members> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);

        Integer total = membersMapper.selectCount(qw);
        return total <= 0;
    }

    @Override
    public Users getOne(String id) {
        return usersMapper.selectById(id);
    }

    /**
     * 依据用户名获取用户信息
     *
     * @param userName 用户账号
     * @return 用户信息
     */
    @Override
    public Users getUserByUserName(String userName) {
        QueryWrapper<Users> qw = new QueryWrapper<>();
        qw.eq("user_name", userName);

        return usersMapper.selectOne(qw);
    }

    /**
     * 分页查询系统用户信息
     *
     * @param pageIndex 当前页码
     * @param pageSize  每页数据量
     * @param users     模糊查询条件
     * @return 用户信息
     */
    @Override
    public PageWrapper getPageInfo(Long pageIndex, Long pageSize, Users users) {
        QueryWrapper<Users> qw = new QueryWrapper<>();
        if (StrUtil.isNotBlank(users.getUserName())) {
            qw.like("user_name", users.getUserName());
        }

        if (StrUtil.isNotBlank(users.getName())) {
            qw.like("name", users.getName());
        }

        if (StrUtil.isNotBlank(users.getPhone())) {
            qw.like("phone", users.getPhone());
        }

        qw.orderByDesc("create_time");

        Page<Users> page =
                usersMapper.selectPage(new Page<>(pageIndex, pageSize), qw);

        return parsePage(page);
    }

    /**
     * 转化分页查询的结果
     */
    public PageWrapper parsePage(Page<Users> p) {
        List<Map<String, Object>> res = new ArrayList<>();
        for (Users users : p.getRecords()) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("id", users.getId());
            temp.put("userName", users.getUserName());
            temp.put("passWord", users.getPassWord());
            temp.put("name", users.getName());
            temp.put("gender", users.getGender());
            temp.put("age", users.getAge());
            temp.put("phone", users.getPhone());
            temp.put("address", users.getAddress());
            temp.put("status", users.getStatus());
            temp.put("createTime", users.getCreateTime());
            temp.put("type", users.getType());
            res.add(temp);
        }

        return new PageWrapper(p.getCurrent(), p.getSize(), p.getTotal(), res);
    }
}